package roborally.program.text;

import java.util.ArrayList;
import java.util.List;

/**
 * A class collecting the static methods for manipulating basic String operations
 * that are not possible using the java API for Strings. 
 * 
 * @author Molderez Tom, 2de bach Ingenieurswetenschappen Computerwetenschappen-Elektrotechniek and Reniers Jorn, 2de bach Ingenieurswetenschappen Werktuigkunde-Computerwetenschappen
 * @version 1.0
 *
 */
public class TextManipulation {

	/**
	 * No object of this class may be created.
	 */
	private TextManipulation() {
		
	}
	
	/**
	 * Splits a word in its composing blocks. 
	 * We call them here sub blocks, but do not confuse them with the sub blocks specified in CommandManager.
	 * A sub block is something of the form: <(> <internal Strings> <)> 
	 * For example in (seq (move) (while (true) (turn))) the sub blocks are (move) and (while (true) (turn))
	 * 
	 * @param word The word of which a sub block must be created.
	 * @return A list of sub blocks that all start with a bracket and ends with a bracket.
	 * @throws IllegalArgumentException
	 * 		When the bracket-structure is not valid.
	 */
	public static List<String> getBlocks(String word) throws IllegalArgumentException {
		List<String> blocks = new ArrayList<String>();
		char[] wordChar = word.trim().toCharArray();
		int left = 0;
		int right = 0;
		String temp = new String();
		for(int i = 0; i < wordChar.length; i++) {
			if(wordChar[i] == '(')
				left++;
			else if(wordChar[i] == ')')
				right++;
			if(left > 0)
				temp = temp + wordChar[i];
			if(left == right && left != 0) {
				blocks.add(temp);
				temp = new String();
				left = 0;
				right = 0;
			}
		}
		if(left != 0 || right != 0)
			throw new IllegalArgumentException("The bracket structure of the given word: " + word + " is not valid.");
		return blocks;
	}
	
	/**
	 * Removes the redundant symbols from an String so that the String can be recognized
	 * by the different patterns.
	 * 
	 * @param word The word of which the redundant symbols must be removed.
	 * @return Replaces all white characters, new lines , ... that are one or more by just one space character.
	 * 		| result == word.replaceAll("\\s+", " " )
	 */
	public static String removeRedundantSymbols(String word) {
		return word.replaceAll("\\s+", " " );
	}
	
	/**
	 * A String representing the amount of spaces that must insert extra for each
	 * level of writing the code nicely.
	 */
	public static final String LEFT_SPACE = "  ";

}
